package com.shizhanzhe.zxbapp.vip;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.base.BaseActivity;
import com.shizhanzhe.zxbapp.bean.Notice;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.customtask.PublishActivity;
import com.shizhanzhe.zxbapp.home.SourceMaterialActivity;
import com.shizhanzhe.zxbapp.pay.PayVIPActivity;
import com.shizhanzhe.zxbapp.task.FastTaskActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.MyListView;

import java.util.List;

/**
 * Created by zz9527 on 2018/10/29.
 */
public class VIPActivity extends BaseActivity implements View.OnClickListener {

    private MyListView notice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        initView();
        getData();
    }

    @Override
    protected void initView() {
        findViewById(R.id.backarea).setOnClickListener(this);
        findViewById(R.id.iv_vipbuy).setOnClickListener(this);
        findViewById(R.id.vip_gaoe).setOnClickListener(this);
        findViewById(R.id.vip_kuaisu).setOnClickListener(this);
        findViewById(R.id.vip_more).setOnClickListener(this);
        findViewById(R.id.vip_sucai).setOnClickListener(this);
        findViewById(R.id.gz).setOnClickListener(this);
        findViewById(R.id.xs).setOnClickListener(this);
        notice = findViewById(R.id.notice);
        findViewById(R.id.publish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.level > 0) {
                    startActivity(new Intent(VIPActivity.this, PublishActivity.class));
                } else {
                    Toast.makeText(VIPActivity.this, "VIP专享", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(VIPActivity.this, PayVIPActivity.class));
                }
            }

        });
    }

    @Override
    protected void getData() {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.NOTICE(1), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    final List<Notice.DataBean> list = gson.fromJson(json, Notice.class).getData();
                    notice.setAdapter(new NoticeAdapter(VIPActivity.this, list));
                    notice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Intent intent = new Intent(VIPActivity.this, NoticeDetailActivity.class);
                            intent.putExtra("title", list.get(i).getTitle());
                            intent.putExtra("id", list.get(i).getId());
                            startActivity(intent);
                        }
                    });
                } else {
                    Toast.makeText(VIPActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backarea:
                finish();
                break;
            case R.id.iv_vipbuy:
                if (UserBean.level > 0) {
                    Toast.makeText(this, "您已是VIP用户", Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(VIPActivity.this, PayVIPActivity.class));
                }
                break;
            case R.id.vip_kuaisu:
                Intent intent = new Intent(VIPActivity.this, FastTaskActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.vip_gaoe:
                Intent intent2 = new Intent(VIPActivity.this, FastTaskActivity.class);
                intent2.putExtra("type", 2);
                startActivity(intent2);
                break;
            case R.id.vip_more:
                Intent intent3 = new Intent(VIPActivity.this, VipMoreActivity.class);
                intent3.putExtra("type", 3);
                startActivity(intent3);
                break;
            case R.id.vip_sucai:
                startActivity(new Intent(VIPActivity.this, SourceMaterialActivity.class));
                break;
            case R.id.gz:
                startActivity(new Intent(VIPActivity.this, NoticeActivity.class).putExtra("type", 2));
                break;
            case R.id.xs:
                startActivity(new Intent(VIPActivity.this, NoticeActivity.class).putExtra("type", 1));
                break;
        }
    }
}
